package com.thh.ftp.util;



import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ValidationData {
    //非空验证
    public static boolean ValidationIsNotEmpty(Object param) {


        if (param == null)//Object类型判空是和null进行对比
        {
            throw new RuntimeException("object is null");
        }
        if ((param instanceof List && ((List) param).size() == 0))//  instanceof  判断对象所属的类型
        {
            throw new RuntimeException("list长度为不得为0");
        }
        if ((param instanceof String && ((String) param).trim().equals(""))) {
           throw new RuntimeException("字符串不得为空");

        }
        return true;

    }

    //非空验证
    public static boolean ValidationIsNotEmptyCheck(Object param) {

        if (param == null)//Object类型判空是和null进行对比
        {
            return false;
        }
        if ((param instanceof List && ((List) param).size() == 0))//  instanceof  判断对象所属的类型
        {
            return false;
        }
        if ((param instanceof String && ((String) param).trim().equals(""))) {
            return false;
        }
        return true;

    }

    //邮箱验证+非空验证
    public static boolean ValidationIsEmail(Object param) {
        ValidationIsNotEmpty(param);
        String email = param.toString();
        String check = "^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        if (!match(check, email)) {
            throw new RuntimeException("输入邮箱不正确");

        }
        return true;
    }


    /**
     * 验证电话号码
     *
     * @param param
     * @return 如果是符合格式的字符串, 返回 <b>true </b>,否则为 <b>false </b>
     */
    public static boolean IsTelephone(Object param) {
        ValidationIsNotEmpty(param);
        String phone = param.toString();
        String check = "^(\\d{3,4}-)?\\d{6,8}$";
        if (!match(check, phone)) {
            throw new RuntimeException("输入电话不正确");

        }
        return true;
    }

    /**
     * 验证电话号码
     *
     * @param param
     * @return 如果是符合格式的字符串, 返回 <b>true </b>,否则为 <b>false </b>
     */
    public static boolean IsHandset(Object param) {
        ValidationIsNotEmpty(param);
        String phone = param.toString();
        String check = "^[1]+[3,5]+\\d{9}$";
        if (!match(check, phone)) {
            throw new RuntimeException("输入电话不正确");

        }
        return true;
    }

    /**
     * 验证输入身份证号
     *
     * @param param
     * @return 如果是符合格式的字符串, 返回 <b>true </b>,否则为 <b>false </b>
     */
    public static boolean IsIDCard(Object param) {
        ValidationIsNotEmpty(param);
        String card = param.toString();
        String check =  "(^\\d{18}$)|(^\\d{15}$)";
        if (!match(check, card)) {
            throw new RuntimeException("输入身份证不合法");

        }
        return true;
    }


    /**
     * @param regex 正则表达式字符串
     * @param str   要匹配的字符串
     * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
     */
    private static boolean match(String regex, String str) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }





}
